/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cip.esymons.eventos.bc;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.cip.esymons.eventos.be.Contrato;
import org.cip.esymons.general.bc.IgvFacadeLocal;
import org.cip.esymons.general.be.Igv;
import org.cip.esymons.tesoreria.be.CuentaIngreso;
import org.cip.esymons.tesoreria.be.TipoMoneda;

/**
 *
 * @author oscar
 */
public class ContratoDelegate {

    private ContratoFacadeLocal local;
    private IgvFacadeLocal localIgv;
    private CuentasTesoreriaFacadeLocal localCuentaIngreso;

    public ContratoDelegate() {
        local = lookupContratoFacade();
        localIgv = lookupIgvFacade();
        localCuentaIngreso = lookupCuentasTesoreriaFacade();
    }

    public List<Contrato> listar_todos() {
        try {
            return local.findAll();
        } catch (Exception e) {
            return new ArrayList<Contrato>();
        }

    }

    public List<Contrato> reporte_fechas(Date ini, Date fin) throws Exception {
        return local.reporteFechas(ini, fin);
    }

    public Contrato buscar_contrato(int cont) {
        try {
            return local.find(cont);
        } catch (Exception e) {
            return new Contrato();
        }

    }

    public void guardar_Contrato(Contrato con) {
        try {
            local.create(con);
        } catch (Exception e) {
            System.out.println("FATAL: " + e.getMessage());
        }
    }

    public void actualizar_Contrato(Contrato con) {
        try {
            local.edit(con);
        } catch (Exception e) {
            System.out.println("FATAL: " + e.getMessage());
        }
    }

    public List<Contrato> listarContratosxAnular() {
        return local.listarContratosxAnular();
    }

    //lizardo estuvo aqui
    public Contrato buscarUltimoContrato() throws Exception {
        return local.buscarUltimoContrato();
    }

    public List<Contrato> listarContratos(Long solCodigo) throws Exception {
        return local.listarContratos(solCodigo);
    }

    public Double calcularSubTotal(Contrato contrato) throws Exception {
        return local.calcularSubTotal(contrato);
    }

    private ContratoFacadeLocal lookupContratoFacade() {
        try {
            Context c = new InitialContext();
            return (ContratoFacadeLocal) c.lookup("java:comp/env/ContratoFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    //lizardo estuvo aqui
    public Igv igvVigente() throws Exception {
        return localIgv.igvVigente();
    }

    private IgvFacadeLocal lookupIgvFacade() {
        try {
            Context c = new InitialContext();
            return (IgvFacadeLocal) c.lookup("java:comp/env/IgvFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    public CuentaIngreso dameCuentaIngresoXAmbienteServicio(boolean tipo, Integer codigo, Long tipoMoneda) throws Exception {
        return localCuentaIngreso.dameCuentaIngresoXAmbienteServicio(tipo, codigo,tipoMoneda);
    }

    public List<TipoMoneda> listarTipoMoneda() throws Exception {
        return localCuentaIngreso.listarTipoMoneda();
    }

    public List<ComprobanteAdapter> recalcularComprobante(Integer conCodigo, Double descuento) throws Exception {
        return local.recalcularComprobante(conCodigo,descuento);
    }

    public List<FactorAdapter> getListaFactores(Integer conCodigo, Double igv, Double factorTotal) throws Exception {
        return local.getListaFactores(conCodigo, igv, factorTotal);
    }

    public List<ComprobanteAdapter> getListaComprobante(Integer conCodigo, Double igv, Double factorTotal) throws Exception {
        return local.getListaComprobante(conCodigo, igv, factorTotal);
    }

    public List<ComprobanteAdapter> getListaComprobante(List<ComprobanteAdapter> listaComprobante, List<FactorAdapter> listaFactores, Double factorToral) {
        return local.getListaComprobante(listaComprobante, listaFactores, factorToral);
    }

    public MontosComprobanteAdapter getMontosComprobante() throws Exception {
        return local.getMontosComprobante();
    }

    public Boolean anularMovimientoCuenta(Integer conCodigo) throws Exception {
        return local.anularMovimientoCuenta(conCodigo);
    }

    private CuentasTesoreriaFacadeLocal lookupCuentasTesoreriaFacade() {
        try {
            Context c = new InitialContext();
            return (CuentasTesoreriaFacadeLocal) c.lookup("java:comp/env/CuentasTesoreriaFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }



}
